package com.mes.basicdata.domain;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Where;
import org.springframework.context.annotation.Lazy;

/**
 * Created on 2017-03-14
 * </p>
 * 
 * @author lcs
 * 
 */
@Entity()
//@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name = "R_RESOURCE")
@Where(clause="RESOURCETYPE = 0")
@BatchSize(size=10)
@Lazy(value=true)
//@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Device extends Resource implements java.io.Serializable {
	public final static int DEVICESTATE_RUNNING = 1;
	public final static int DEVICESTATE_IDLE = 2;
	public final static int DEVICESTATE_MAINTENANCE = 3;
	public final static int DEVICESTATE_REPAREING = 4;

	
	public final static int CRITICALLEVEL_FIRST = 1; //最关键
	public final static int CRITICALLEVEL_SECOND = 2;//较关键
	public final static int CRITICALLEVEL_THIRD = 3;  //一般
	public final static int CRITICALLEVEL_FOURTH = 4;//不关键
	public final static int CRITICALLEVEL_FIFTH = 5; //不关键
	public final static int CRITICALLEVEL_IGNORE = 6;//无限能力


	// 资源编号
	@Id
	@GenericGenerator(name = "deviceUid", strategy = "assigned")
	@Column(name="RESUID")
	private String deviceUid;
	
	// 资源名称
	@Column(name="RESNAME")	
	private String deviceName;
	
	// 设备型号
	@Column(name="MODEL_NAME")	
	private String modelName;
	
	// 资源类型
	@Column(name="RESOURCETYPE")	
	private String deviceType = "0";

	@Column(name="CUSTOMER_RESUID")	
	private String customerResUid;
	
	// 位置
	@Column(name="LOCATION")	
	private String location;
	
	// 资源池数量
	@Column(name="NUM_OF_POOLED_RESOURCES",precision=10,scale=0 )
	private Long numOfPooledResources;
	
	// 理想利用率
	@Column(name="IDEAL_UTILIZATION_LEVEL",precision=18,scale=4 )
	private Double idealUtilizationLevel;
	
	// 默认日历
	@Column(name="CAL_UID")
	private String defaultCalendarUid;
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="CAL_UID",insertable=false,updatable=false)
	private Calendar defaultCalendar;
	
//	//设备类型
//	@Column(name="RESTYPE")	
//	private String deviceClassUid;
//	private String deviceClassName;
//	
//	// 估计排队等待时间
//	private Long estimatedQueueTime;
//	// 估计排队等待时间单位
//	private String estimatedQueueTimeUom;
	
//	// 默认安装时间
//	@Column(name="DEFAULT_SETUP_TIME",precision=10,scale=0 )
//	private Long defaultSetupTime;
//	
//	// 默认安装时间单位
//	@Column(name="DEFAULT_TIME_UOM")	
//	private String defaultTimeUom;
//	
//	private String idealUtilCalendarName;
//	// 费用率
//	private String wageRatePer;
//	// 每次工资率
//	private Double wageRatePerTime;
//	// 安装矩阵号
//	@Column(name="SETUP_MATRIX_ID")	
//	private String setupMatrixId;
//	// 安装队列编号
//	private String setupSequenceId;
//	// 允许反向安装序列
//	private Long setupSequenceGoback;
//	// 最小队列时间
//	private Long minimumQueueTime;
//	// 最小队列时间单位
//	private String minimumQueueTimeUom;
	
//	// 调度冻结水平
//	private Long scheduleFreezeLevel;
//	// 调度冻结水平单位
//	private String scheduleFreezeLevelUom;
//	// 分批水平
//	private Long batchingLevel;
//	// 分批水平单位
//	private String batchingLevelUom;
//	// 分批提前期
//	private Long batchingLookAhead;
//	// 合批考虑提前时间单位
//	private String batchingLookAheadUom;
//	// 拖期容差
//	private Long latenessTolerance;
//	// 拖期容差单位
//	private String latenessToleranceUom;
//	// 提前容差
//	private Long earlinessTolerance;
//	// 提前容差单位
//	private String earlinessToleranceUom;
//	// 队列大小模式
//	private String queueSizeMode;
//	// 最大队列数量
//	private String maxQueueSize;
//	// 队列一致模式
//	private String queueConformanceMode;
//	// 产出率限制
//	private Long outputRateLimit;
//	// 每台产出率限制
//	private String outputRateLimitPer;
//	// 每台产出率限制单位
//	private String outputRateLimitUom;
//	// 最小流量警告范围
//	private Long lowRwt;
//	// 每台最小流量范围
//	private String lowRwtPer;
//	// 最小流量范围单位
//	private String lowRwtUom;
//	// 最高流量范围单位
//	private String highRwtUom;
//	// 最高流量范围单位
//	private String highRwtPer;
//	// 最高流量范围
//	private Long highRwt;
//	// 增加取消修改标志
//	private String acmFlag;
//	// 每台增量成本
//	private String overtimeCostPer;
//	// 每次增量成本
//	@Column(name="INC_OVERTIME_COST_PER_T")	
//	private String overtimeCostPerTime;
	
	// 上次维修日期
	@Column(name="LAST_MAINT_DATE",precision=10,scale=0 )
	private Long lastMaintenanceDate;
	
//	// 上次维修日期格式
//	@Column(name="LAST_MAINT_DATE_FORMAT")	
//	private String lastMaintenanceDateFormat;
//	// 上次维修序列号
//	@Column(name="LAST_MAINT_SEQ_NUMBER")	
//	private String lastMaintenanceSequenceNumber;
//	// 上次维修时间
//	private Long lastMaintenanceTime;
//	// 上次维修时间单位
//	private String lastMaintenanceTimeUom;
//	// 从上次维修后负载大小
//	private Long loadSinceLastMaintenance;
//	// 负载单位
//	private String loadSinceUom;
//	// 最大利用等级
//	private Long maximumUtilLevel;
//	// 最小优化日历名称
//	private String minOpTimeCalName;
//	// 最小等待配置长度
//	private Date minQueueConfigLength;
//	// 配置长度单位
//	private String minQueueConfigUom;
//	// 最小利用等级
//	private Long minimumUtilizationLevel;
//	// 这个资源自动卸载
//	// private Long offloadableFrom;
//	private Long offloadableFrom;
//	// 其它资源自动卸载
//	private Long offloadableTo;
//	// 超期基准
//	private Long overtimeBasis;
//	// 超期基准单位
//	private String overtimeBasisUom;
//	// 超期范围
//	private Long overtimeScope;
//	// 超期范围单位
//	private String overtimeScopeUom;
//	// 物料处理类型
//	private String processedMaterialTypes;
//	// 排队大小单位
//	private String queueSizeUom;
//	// 调度关键性
//	private Long schedulingCriticality;
//	// 单台安装成本
//	private String unitSetupCostPer;
//	// 单次安装成本
//	private Double unitSetupCostPerTime;
//	// CAO相关
//	private Long caoPostProcDownstream;
//	// 保持相关任务在一起
//	private Long keepRelatedTasksTog;
//	// 最小负载率
//	private Long preIcpOlRuntimeRatio;
//	// 同步层次化安装
//	private Long simulHierSetups;
//	// 护绿能力计划
//	private Long ignoreForCapPlan;
	
	// 派工等级标记
	@Column(name="NONENEEDDISPATCH",precision=2,scale=0 )
	private Long dispatchGrade;
	
	//设备状态
	@Column(name="RESSTATE",precision=1,scale=0 )
	private Long deviceState;
	
//	// 资源参考文档
//	@Column(name="RESOURCEREF")
//	private String deviceReference;
	
	// 资源关键等级
	@Column(name="CRITICAL_LEVEL",precision=2,scale=0 )
	private Long criticalLevel;
	
//	// 需要NC代码
//	@Column(name="NEED_NC",precision=10,scale=3)
//	private Double needNc;
//	// 月计划工作时间
//	@Column(name="MONTHPLANWORK",precision=10,scale=1)
//	private Double monthPlanWorkTime;
	
	// 工种类型
	@Column(name="OPTYPE")
	private String workType;
	
	// 对应的操作者
	@Transient
	private Set<Employee> operators = new HashSet<Employee>(0);
	@Transient
	private Set<EmployeeDeviceCertification> employeeDeviceCertificates = new HashSet<EmployeeDeviceCertification>(0);
	
	/**
	 *当前设备的操作者
	 *many-to-one 
	 * **/
	@Column(name="CURRENT_EMPLOYEEID")
	private String currentOperatorUid;
	@ForeignKey(name="null")
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="CURRENT_EMPLOYEEID",insertable=false,updatable=false)	
	private Employee currentOperator;

	// 工作台数量
	@Column(name="DEVICE_TABLE_COUNT",precision=10,scale=0)
	private Long deviceTableCount;
	
	private Double usableAbility;
	
	/**
	 * 照片图像
	 */
	@Column(name="IMAGEURL")
	protected String imageUrl;

	// 设备终端对应的ip地址
	@Column(name="DEVICEIP")
	private String deviceIP;
	
	
	@Override
	public String getUuid() {
		// TODO Auto-generated method stub
		return this.getDeviceUid();
	}

	@Override
	public void setUuid(String uuid) {
		// TODO Auto-generated method stub
		this.setDeviceUid(uuid);
	}

	public String getDeviceIP() {
		if (this.deviceIP == null) {
			// 应该由设备表中一个字段取代
			// 现在是庆安临时用法
			if (deviceUid != null && deviceUid.length() == 12) {
				deviceIP = deviceUid.substring(0, 3) + "."
						+ deviceUid.substring(3, 6) + "."
						+ deviceUid.substring(6, 9) + "."
						+ deviceUid.substring(9, 12);
			}
		}
		return deviceIP;
	}

	public void setDeviceIP(String deviceIP) {
		this.deviceIP = deviceIP;
	}

	public String getDeviceUid() {
		return deviceUid;
	}

	public void setDeviceUid(String deviceUid) {
		this.deviceUid = deviceUid;
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}

	public String getModelName() {
		return modelName;
	}

	public void setModelName(String modelName) {
		this.modelName = modelName;
	}

	public String getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(String deviceType) {
		this.deviceType = deviceType;
	}

	public String getCustomerResUid() {
		return customerResUid;
	}

	public void setCustomerResUid(String customerResUid) {
		this.customerResUid = customerResUid;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Long getNumOfPooledResources() {
		return numOfPooledResources;
	}

	public void setNumOfPooledResources(Long numOfPooledResources) {
		this.numOfPooledResources = numOfPooledResources;
	}

	public Double getIdealUtilizationLevel() {
		return idealUtilizationLevel;
	}

	public void setIdealUtilizationLevel(Double idealUtilizationLevel) {
		this.idealUtilizationLevel = idealUtilizationLevel;
	}

	public String getDefaultCalendarUid() {
		return defaultCalendarUid;
	}

	public void setDefaultCalendarUid(String defaultCalendarUid) {
		this.defaultCalendarUid = defaultCalendarUid;
	}

	public Calendar getDefaultCalendar() {
		return defaultCalendar;
	}

	public void setDefaultCalendar(Calendar defaultCalendar) {
		this.defaultCalendar = defaultCalendar;
	}

	public Long getLastMaintenanceDate() {
		return lastMaintenanceDate;
	}

	public void setLastMaintenanceDate(Long lastMaintenanceDate) {
		this.lastMaintenanceDate = lastMaintenanceDate;
	}

	public Long getDispatchGrade() {
		return dispatchGrade;
	}

	public void setDispatchGrade(Long dispatchGrade) {
		this.dispatchGrade = dispatchGrade;
	}

	public Long getDeviceState() {
		return deviceState;
	}

	public void setDeviceState(Long deviceState) {
		this.deviceState = deviceState;
	}

	public Long getCriticalLevel() {
		return criticalLevel;
	}

	public void setCriticalLevel(Long criticalLevel) {
		this.criticalLevel = criticalLevel;
	}

	public String getWorkType() {
		return workType;
	}

	public void setWorkType(String workType) {
		this.workType = workType;
	}

	public Set<Employee> getOperators() {
		return operators;
	}

	public void setOperators(Set<Employee> operators) {
		this.operators = operators;
	}

	public Set<EmployeeDeviceCertification> getEmployeeDeviceCertificates() {
		return employeeDeviceCertificates;
	}

	public void setEmployeeDeviceCertificates(Set<EmployeeDeviceCertification> employeeDeviceCertificates) {
		this.employeeDeviceCertificates = employeeDeviceCertificates;
	}

	public String getCurrentOperatorUid() {
		return currentOperatorUid;
	}

	public void setCurrentOperatorUid(String currentOperatorUid) {
		this.currentOperatorUid = currentOperatorUid;
	}

	public Employee getCurrentOperator() {
		return currentOperator;
	}

	public void setCurrentOperator(Employee currentOperator) {
		this.currentOperator = currentOperator;
	}

	public Long getDeviceTableCount() {
		return deviceTableCount;
	}

	public void setDeviceTableCount(Long deviceTableCount) {
		this.deviceTableCount = deviceTableCount;
	}

	public Double getUsableAbility() {
		return usableAbility;
	}

	public void setUsableAbility(Double usableAbility) {
		this.usableAbility = usableAbility;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	
}